package com.yuanqi.java.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanqi.java.dto.PurchaseReturnOrderDTO;
import com.yuanqi.java.dto.PurchaseReturnOrderVO;
import com.yuanqi.java.entity.PurchaseReturnOrder;
import com.yuanqi.java.result.Result;
import com.yuanqi.java.service.IPurchaseReturnOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/purchase-return-order")
@RequiredArgsConstructor
public class PurchaseReturnOrderController {

	private final IPurchaseReturnOrderService purchaseReturnOrderService;

	/**
	 * 分页查询退货单
	 */
	@GetMapping("/page")
	public Result<Map<String, Object>> page(@RequestParam(defaultValue = "1") long current,
	                                       @RequestParam(defaultValue = "10") long size,
	                                       @RequestParam(required = false) Integer status,
	                                       @RequestParam(required = false) String returnNo) {
		Page<PurchaseReturnOrder> page = new Page<>(current, size);
		IPage<PurchaseReturnOrderVO> resultPage = purchaseReturnOrderService.getReturnOrderPage(page, status, returnNo);
		Map<String, Object> data = new HashMap<>();
		data.put("records", resultPage.getRecords());
		data.put("total", resultPage.getTotal());
		return Result.success(data);
	}

	/**
	 * 创建退货单
	 */
	@PostMapping("/create")
	public Result<Long> create(@RequestBody PurchaseReturnOrderDTO dto) {
		Long id = purchaseReturnOrderService.createReturnOrder(dto);
		return Result.success(id);
	}

	/**
	 * 获取退货单详情
	 */
	@GetMapping("/{id}")
	public Result<PurchaseReturnOrderVO> detail(@PathVariable("id") Long id) {
		PurchaseReturnOrderVO detail = purchaseReturnOrderService.getReturnOrderDetail(id);
		return Result.success(detail);
	}

	/**
	 * 提交审核
	 */
	@PostMapping("/submit/{id}")
	public Result<Boolean> submit(@PathVariable("id") Long id) {
		purchaseReturnOrderService.submitForAudit(id);
		return Result.success(true);
	}

	/**
	 * 删除退货单
	 */
	@DeleteMapping("/{id}")
	public Result<Boolean> delete(@PathVariable("id") Long id) {
		purchaseReturnOrderService.deleteReturnOrder(id);
		return Result.success(true);
	}

	/**
	 * 批量删除退货单
	 */
	@DeleteMapping("/batch")
	public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
		purchaseReturnOrderService.batchDeleteReturnOrder(ids);
		return Result.success(true);
	}
} 